package com.yc.testThread;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BaseThread {
	
	private final static Logger logger = LoggerFactory.getLogger(BaseThread.class);
	
	public void test1() {
		TestThread tt1 = new TestThread();
		tt1.setName("myThread1");
		tt1.start();
		
		TestThread tt2 = new TestThread();
		tt2.setName("myThread2");
		tt2.start();
		
		Thread tt3 = new Thread(new myRunable());
		tt3.setName("myThread3");
		tt3.start();
	}
	
	private void delFile(String filePath) {
		File f = new File(filePath);
		f.delete();
	}

	public Thread[] findAllThread() {
		ThreadGroup currentGroup = Thread.currentThread().getThreadGroup();

		while (currentGroup.getParent() != null) {
			// 返回此线程组的父线程组
			currentGroup = currentGroup.getParent();
		}
		// 此线程组中活动线程的估计数
		int noThreads = currentGroup.activeCount();

		Thread[] lstThreads = new Thread[noThreads];
		// 把对此线程组中的所有活动子组的引用复制到指定数组中。
		currentGroup.enumerate(lstThreads);

		for (Thread thread : lstThreads) {
			logger.info("线程数量：" + noThreads + " 线程id：" + thread.getId() + " 线程名称：" + thread.getName() + " 线程状态："
					+ thread.getState());
		}
		return lstThreads;
	}
	
	public void m2() {
		Thread t1 = new Thread(new changeFile(1));
		t1.start();
		try {
			t1.join();
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		logger.info("--------开始删除");
		deleteDir(new File("/home/newdev/abc"));
		logger.info("--------删除完毕");
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		logger.info("--------over");
	}
	
	public boolean deleteDir(File dir) {
        if (dir.isDirectory()) {
            String[] children = dir.list();
            //递归删除目录中的子目录下
            for (int i=0; i<children.length; i++) {
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        }
        // 目录此时为空，可以删除
        return dir.delete();
	}
	

	public static void main(String[] args) {
		BaseThread bt = new BaseThread();
//		bt.test1();
//		bt.findAllThread();
		bt.m2();

	}
	
	

}

class myRunable implements Runnable {
	@Override
	public void run() {
		System.out.println("---------11-----"+ Thread.currentThread().getName());
		try {
			Thread.sleep(30000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("---------22-----"+ Thread.currentThread().getName());
		
	}
}

class changeFile implements Runnable {
	String filePath = null;
	
	public changeFile(int fn) {
		if (fn == 1) {
			filePath = "/home/newdev/abc/aa.txt";
		} else {
			filePath = "F:/temp/abc/bb.txt";
		}
	}

	@Override
	public void run() {
		String cc = null;
		File f = null;
		FileInputStream fs = null;
		for (int i = 0; i < 20; i++) {
			f = new File(filePath);
			try {
				fs = new FileInputStream(f);
				System.out.println(Thread.currentThread().getName() + "-------" + i);
				Thread.sleep(1000);
			} catch (Exception e) {
				System.out.println("----------捕获到了--------");
				cc.indexOf("a");
			} finally {
				if (null != fs) {
					try {
						fs.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				
			}
			
		}
		
		
	}
	
}


